package com.zfsoft.zfoa.resource.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import com.zfsoft.util.Tool;
import com.zfsoft.zfoa.dao.BasicDAO;
import com.zfsoft.zfoa.resource.entitys.Resource_info;
import com.zfsoft.zfoa.resource.entitys.Resource_useinfo;

public class Resource_useinfoDAO extends BasicDAO {
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.zfsoft.zfoa.resource.dao.Resource_useinfoDAO#getInstance(java.lang
	 * .String)
	 */
	public Resource_useinfo getInstance(String bh) {
		Resource_useinfo resource_useinfo = new Resource_useinfo();
		try {
			String sql = "select * from resource_useinfo where id=?";
			conn = this.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, bh);
			rs = ps.executeQuery();
			if (rs.next()) {
				resource_useinfo.setId(rs.getString("id"));
				resource_useinfo.setTitle(rs.getString("title"));
				resource_useinfo.setDatestart(rs.getString("datestart"));
				resource_useinfo.setDateend(rs.getString("dateend"));
				resource_useinfo.setTimestart(rs.getString("timestart"));
				resource_useinfo.setTimeend(rs.getString("timeend"));
				resource_useinfo.setUserid(rs.getString("userid"));
				resource_useinfo.setNote(rs.getString("note"));
				resource_useinfo.setStatus(rs.getString("status"));
				resource_useinfo.setTablename(rs.getString("tablename"));
				resource_useinfo.setRes_name(rs.getString("res_name"));
				resource_useinfo.setInfoid(rs.getString("infoid"));
				resource_useinfo.setType(rs.getString("type"));
				resource_useinfo.setRes_id(rs.getString("res_id"));
			}
			closeStmt();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConn();
		}
		return resource_useinfo;
	}
	
	
	public Resource_useinfo getResource_infoByInfoid(String bh) {
		Resource_useinfo resource_useinfo = new Resource_useinfo();
		try {
			String sql = "select * from resource_useinfo where infoid=?";
			conn = this.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, bh);
			rs = ps.executeQuery();
			if (rs.next()) {
				resource_useinfo.setId(rs.getString("id"));
				resource_useinfo.setTitle(rs.getString("title"));
				resource_useinfo.setDatestart(rs.getString("datestart"));
				resource_useinfo.setDateend(rs.getString("dateend"));
				resource_useinfo.setTimestart(rs.getString("timestart"));
				resource_useinfo.setTimeend(rs.getString("timeend"));
				resource_useinfo.setUserid(rs.getString("userid"));
				resource_useinfo.setNote(rs.getString("note"));
				resource_useinfo.setStatus(rs.getString("status"));
				resource_useinfo.setTablename(rs.getString("tablename"));
				resource_useinfo.setRes_name(rs.getString("res_name"));
				resource_useinfo.setInfoid(rs.getString("infoid"));
				resource_useinfo.setType(rs.getString("type"));
				resource_useinfo.setRes_id(rs.getString("res_id"));
			}
			closeStmt();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConn();
		}
		return resource_useinfo;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.zfsoft.zfoa.resource.dao.Resource_useinfoDAO#update(java.lang.String,
	 * com.zfsoft.zfoa.resource.entitys.Resource_useinfo)
	 */
	public Boolean update(String bh, Resource_useinfo resource_useinfo) {
		Boolean res = true;
		String sql = "";
		try {
			sql = "UPDATE resource_useinfo SET id=?,title=?,datestart=?,dateend=?,timestart=?,timeend=?,userid=?,note=?,status=?,tablename=?,res_name=?,infoid=?,type=?,res_id=? WHERE id=?";
			conn = this.getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, resource_useinfo.getId());
			ps.setString(2, resource_useinfo.getTitle());
			ps.setString(3, resource_useinfo.getDatestart());
			ps.setString(4, resource_useinfo.getDateend());
			ps.setString(5, resource_useinfo.getTimestart());
			ps.setString(6, resource_useinfo.getTimeend());
			ps.setString(7, resource_useinfo.getUserid());
			ps.setString(8, resource_useinfo.getNote());
			ps.setString(9, resource_useinfo.getStatus());
			ps.setString(10, resource_useinfo.getTablename());
			ps.setString(11, resource_useinfo.getRes_name());
			ps.setString(12, resource_useinfo.getInfoid());
			ps.setString(13, resource_useinfo.getType());
			ps.setString(14, resource_useinfo.getRes_id());
			ps.setString(15, bh);
			ps.executeUpdate();
			closeStmt();
		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				System.out.println(sql);
				e1.printStackTrace();
			}
			res = false;
		} finally {
			try {
				conn.commit();
			} catch (SQLException e) {
				System.out.println(sql);
				e.printStackTrace();
			}
			closeConn();
		}
		return res;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.zfsoft.zfoa.resource.dao.Resource_useinfoDAO#insert(com.zfsoft.zfoa
	 * .resource.entitys.Resource_useinfo)
	 */
	public Boolean insert(Resource_useinfo resource_useinfo) {
		Boolean res = true;
		String sql = "";
		try {
			sql = "insert into resource_useinfo(id,title,datestart,dateend,timestart,timeend,userid,note,status,tablename,res_name,infoid,type,res_id) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			conn = this.getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, resource_useinfo.getId());
			ps.setString(2, resource_useinfo.getTitle());
			ps.setString(3, resource_useinfo.getDatestart());
			ps.setString(4, resource_useinfo.getDateend());
			ps.setString(5, resource_useinfo.getTimestart());
			ps.setString(6, resource_useinfo.getTimeend());
			ps.setString(7, resource_useinfo.getUserid());
			ps.setString(8, resource_useinfo.getNote());
			ps.setString(9, resource_useinfo.getStatus());
			ps.setString(10, resource_useinfo.getTablename());
			ps.setString(11, resource_useinfo.getRes_name());
			ps.setString(12, resource_useinfo.getInfoid());
			ps.setString(13, resource_useinfo.getType());
			ps.setString(14, resource_useinfo.getRes_id());
			ps.executeUpdate();
			closeStmt();
		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				System.out.println(sql);
				e1.printStackTrace();
			}
			res = false;
		} finally {
			try {
				conn.commit();
			} catch (SQLException e) {
				System.out.println(sql);
				e.printStackTrace();
			}
			closeConn();
		}
		return res;
	}
	public boolean saveOrupdateResouce(List<Resource_useinfo> list) {
		boolean res = false;
		Vector<HashMap<String, Object>> vec = new Vector<HashMap<String, Object>>();
		String sqlin = "insert into resource_useinfo(title,id,res_name,datestart,dateend,timestart,timeend,userid,note,tablename,type,infoid,res_id,status) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		String sqldel = " delete from resource_useinfo where infoid = ?";
		HashMap<String, Object> map = null;
		if (!list.isEmpty()) {
			map = new HashMap<String, Object>();
			map.put("sqlTxt", sqldel);
			map.put("sqlVal", new String[] { list.get(0).getInfoid() });
			vec.add(map);
			for (int i = 0, n = list.size(); i < n; i++) {
				map = new HashMap<String, Object>();
				map.put("sqlTxt", sqlin);
				map.put("sqlVal", new String[] { list.get(i).getTitle(),Tool.getId(),
						list.get(i).getRes_name(), list.get(i).getDatestart(),
						list.get(i).getDateend(), list.get(i).getTimestart(),
						list.get(i).getTimeend(), list.get(i).getUserid(),
						list.get(i).getNote(), list.get(i).getTablename(),
						list.get(i).getType(), list.get(i).getInfoid(),list.get(i).getRes_id(),
						list.get(i).getStatus() });
				vec.add(map);
			}
			res = this.runUpdate(vec);
		}
		return res;
	}
	
	
	public boolean saveOrupdateResouce(Resource_useinfo resource_useinfo) {
		boolean res = false;
		String sql = "";
		String id = "";
		if (Tool.isNull(resource_useinfo.getId())) {
			sql = "insert into resource_useinfo(res_name,datestart,dateend,timestart,timeend,userid,note,tablename,type,infoid,res_id,status,title,id) "
					+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
			id = Tool.getId();
		} else {
			sql = "update resource_useinfo set res_name=?,datestart=?,dateend=?,timestart=?,timeend=?,userid=?,note=?,tablename=?,type=?,infoid=?,res_id=?,status=?,title=? WHERE id=?";
			id = resource_useinfo.getId();
		}
		res = this.runUpdate(sql, new String[] {
				resource_useinfo.getRes_name(),
				resource_useinfo.getDatestart(), resource_useinfo.getDateend(),
				resource_useinfo.getTimestart(), resource_useinfo.getTimeend(),
				resource_useinfo.getUserid(), resource_useinfo.getNote(),
				resource_useinfo.getTablename(), resource_useinfo.getType(),
				resource_useinfo.getInfoid(), resource_useinfo.getRes_id(),
				resource_useinfo.getStatus(),resource_useinfo.getTitle(), id });
		return res;
	}

	public List<Resource_useinfo> getInstanceList(String infoid) {
		List<Resource_useinfo> list = new ArrayList<Resource_useinfo>();
		String sql = "select * from resource_useinfo where infoid = ?";
		try {
			conn = this.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, infoid);
			rs = ps.executeQuery();
			while (rs.next()) {
				Resource_useinfo resource_useinfo = new Resource_useinfo();
				resource_useinfo.setId(rs.getString("id"));
				resource_useinfo.setRes_name(rs.getString("res_name"));
				resource_useinfo.setDatestart(rs.getString("datestart"));
				resource_useinfo.setDateend(rs.getString("dateend"));
				resource_useinfo.setTimestart(rs.getString("timestart"));
				resource_useinfo.setTimeend(rs.getString("timeend"));
				resource_useinfo.setUserid(rs.getString("userid"));
				resource_useinfo.setNote(rs.getString("note"));
				resource_useinfo.setTablename(rs.getString("tablename"));
				resource_useinfo.setType(rs.getString("type"));
				resource_useinfo.setInfoid(rs.getString("infoid"));
				resource_useinfo.setRes_id(rs.getString("res_id"));
				resource_useinfo.setStatus(rs.getString("status"));
				list.add(resource_useinfo);
			}
			closeStmt();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConn();
		}
		return list;

	}

	public boolean deleteResource_useinfoByInfoid(String infoid) {
		String sql = "delete from resource_useinfo where infoid = ?";
		return this.runUpdate(sql, new String[] { infoid });
	}

	public boolean updateStatus(String infoid) {
		String sql ="update resource_useinfo set status = '1' where infoid = ?";
		return this.runUpdate(sql, new String[] { infoid });
	}


	
	public List<Resource_info> getAllResourceList(String type){
		List<Resource_info> list = new ArrayList<Resource_info>();
		String sql="";
		if(Tool.isNull(type)){
			sql = "select * from resource_info where 1=1 order by to_number(zindex)";
		}else{
			sql = "select * from resource_info where type =? order by to_number(zindex)";
		}
		//String sql = "select res_id id,res_name name,yhm managerid,xm managername,'' status,'meet_room' type from resource_man";
		try{
			conn = this.getConn();
			ps = conn.prepareStatement(sql);
			if(!Tool.isNull(type)){
				ps.setString(1, type);
			}			
			rs = ps.executeQuery();
			while(rs.next()){
				Resource_info resource = new Resource_info();
				resource.setId(rs.getString("id"));
				resource.setName(rs.getString("name"));
				resource.setManagerid(rs.getString("managerid"));
				resource.setType(rs.getString("type"));
				resource.setStatus(rs.getString("status"));
				resource.setManagername(rs.getString("managername"));
				list.add(resource);
			}
			closeStmt();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConn();
		}
		return list;
	}
	
	public List<Resource_useinfo> getListBytimeAndId(String res_id,String time){
		List<Resource_useinfo> list = new ArrayList<Resource_useinfo>();
		String sql = "select * from resource_useinfo where res_id = ? and datestart <= ? and dateend>=? order by timestart";
		try{
			conn = this.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1,res_id);
			ps.setString(2, time);ps.setString(3, time);
			rs = ps.executeQuery();
			while(rs.next()){
				Resource_useinfo resource = new Resource_useinfo();
				resource.setRes_id(rs.getString("res_id"));
				resource.setRes_name(rs.getString("res_name"));
				resource.setDatestart(rs.getString("datestart"));
				resource.setDateend(rs.getString("dateend"));
				resource.setId(rs.getString("id"));
				resource.setInfoid(rs.getString("infoid"));
				resource.setTimeend(rs.getString("timeend"));
				resource.setTimestart(rs.getString("timestart"));
				resource.setStatus(rs.getString("status"));
				resource.setTitle(rs.getString("title"));
				list.add(resource);
			}
			closeStmt();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConn();
		}
		return list;
	}
	public boolean deleteByInforid(String infoid){
		Boolean res = true;
		String sql = "";
		try {
			sql = "delete from resource_useinfo where infoid =?";
			conn = this.getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1,infoid);
			ps.executeUpdate();
			closeStmt();
		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				System.out.println(sql);
				e1.printStackTrace();
			}
			res = false;
		} finally {
			try {
				conn.commit();
			} catch (SQLException e) {
				System.out.println(sql);
				e.printStackTrace();
			}
			closeConn();
		}
		return res;
	}
}